package com.zhugang.week04;

/**
 * @program algorithms
 * @description: exchange
 * @author: chanzhugang
 * @create: 2022/06/30 10:21
 */
public class Exchange {

    /**
     * 剑指offer 21 调整数组顺序使奇数位于偶数前面
     *
     * @param nums
     * @return
     */
    public int[] exchange(int[] nums) {
        // 双指针i、j，不满足条件交换i、j
        int i = 0;
        int j = nums.length;
        while (i < j) {
            if (nums[i] % 2 == 1) {
                i++;
                continue;
            }
            if (nums[j] % 2 == 0) {
                j--;
                continue;
            }
            // nums[i]是偶数，nums[j]是奇数，swap
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            i++;
            j--;
        }
        return nums;
    }
}